package com.quick.send.contact.model;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import com.quick.send.contact.activity.R;


public class ContactCustomAdapter extends ArrayAdapter<Contact> implements
		Filterable {

	private List<Contact> originalContactList = new ArrayList<Contact>();
	private List<Contact> filterContactList = new ArrayList<Contact>();
	private Context context;
	private ContactFilter contactFilter = new ContactFilter();

	public ContactCustomAdapter(Context context, List<Contact> resource) {
		super(context, R.layout.contact_list, resource);
		this.context = context;
		// Clear selected
		for (Contact contact : resource) {
			contact.setSelected(false);
			this.originalContactList.add(contact);
			this.filterContactList.add(contact);
		}

	}

	public List<Contact> getOriginalContactList() {
		return originalContactList;
	}

	@Override
	public int getCount() {
		return filterContactList.size();
	}

	@Override
	public Contact getItem(int position) {
		return filterContactList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public Filter getFilter() {
		if (contactFilter == null) {
			contactFilter = new ContactFilter();
		}
		return contactFilter;
	}

	public List<Contact> getFilterContactList() {
		return filterContactList;
	}

	private class ViewHolder {
		TextView displayName;
		CheckBox selectedCheckBox;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder = null;

		if (convertView == null) {

			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.contact_list, null);

			holder = new ViewHolder();
			holder.displayName = (TextView) convertView
					.findViewById(R.id.displayNameTextView);
			holder.selectedCheckBox = (CheckBox) convertView
					.findViewById(R.id.selectedCheckBox);
			convertView.setTag(holder);
			//
			convertView.setClickable(true);
			convertView.setOnClickListener(itemClickListener);

			holder.selectedCheckBox
					.setOnClickListener(new View.OnClickListener() {
						public void onClick(View v) {
							CheckBox cb = (CheckBox) v;
							Contact contact = (Contact) cb.getTag();
							contact.setSelected(cb.isChecked());
						}
					});
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Contact contact = filterContactList.get(position);
		holder.displayName.setText(contact.getDisplayName());
		holder.selectedCheckBox.setChecked(contact.isSelected());
		holder.selectedCheckBox.setTag(contact);
		return convertView;

	}

	@SuppressLint("DefaultLocale")
	private class ContactFilter extends Filter {
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			FilterResults results = new FilterResults();
			if (constraint == null || constraint.length() == 0) {
				// No input text for filter, will return the original list
				synchronized (this) {
					results.values = originalContactList;
					results.count = originalContactList.size();
				}

			} else {

				String filterString = constraint.toString().toLowerCase();

				final ArrayList<Contact> filterList = new ArrayList<Contact>();

				for (Contact contact : originalContactList) {
					if (contact.getDisplayName().toLowerCase().contains(filterString)) {
						filterList.add(contact);
					}
				}

				results.values = filterList;
				results.count = filterList.size();
			}
			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			if (results.count == 0) {
				notifyDataSetInvalidated();
			} else {
				filterContactList = (List<Contact>) results.values;
				notifyDataSetChanged();
			}

		}
	}

	// When user click on item then set check box is checked otherwise
	public OnClickListener itemClickListener = new OnClickListener() {
		public void onClick(View v) {

			CheckBox checkItem = (CheckBox) v
					.findViewById(R.id.selectedCheckBox);
			checkItem.setChecked(!checkItem.isChecked());

			Contact contact = (Contact) checkItem.getTag();
			contact.setSelected(checkItem.isChecked());
		}
	};

}
